home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / acciap1g / registry.bas < prev   
Encoding:
BASIC Source File  |  1999-07-24  |  13.7 KB  |  347 lines

  1. Attribute VB_Name = "WR"
  2. Option Explicit
  3.  
  4. Type FILETIME
  5.     lLowDateTime    As Long
  6.     lHighDateTime   As Long
  7. End Type
  8.  
  9. Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  10. Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  11. Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  12. Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  13. Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
  14. Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As Long, lpcbData As Long) As Long
  15. Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  16. Declare Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
  17. Declare Function RegSetValueExB Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Byte, ByVal cbData As Long) As Long
  18.  
  19. Const ERROR_SUCCESS = 0&
  20. Const ERROR_BADDB = 1009&
  21. Const ERROR_BADKEY = 1010&
  22. Const ERROR_CANTOPEN = 1011&
  23. Const ERROR_CANTREAD = 1012&
  24. Const ERROR_CANTWRITE = 1013&
  25. Const ERROR_OUTOFMEMORY = 14&
  26. Const ERROR_INVALID_PARAMETER = 87&
  27. Const ERROR_ACCESS_DENIED = 5&
  28. Const ERROR_NO_MORE_ITEMS = 259&
  29. Const ERROR_MORE_DATA = 234&
  30.  
  31. Const REG_NONE = 0&
  32. Const REG_SZ = 1&
  33. Const REG_EXPAND_SZ = 2&
  34. Const REG_BINARY = 3&
  35. Const REG_DWORD = 4&
  36. Const REG_DWORD_LITTLE_ENDIAN = 4&
  37. Const REG_DWORD_BIG_ENDIAN = 5&
  38. Const REG_LINK = 6&
  39. Const REG_MULTI_SZ = 7&
  40. Const REG_RESOURCE_LIST = 8&
  41. Const REG_FULL_RESOURCE_DESCRIPTOR = 9&
  42. Const REG_RESOURCE_REQUIREMENTS_LIST = 10&
  43.  
  44. Const KEY_QUERY_VALUE = &H1&
  45. Const KEY_SET_VALUE = &H2&
  46. Const KEY_CREATE_SUB_KEY = &H4&
  47. Const KEY_ENUMERATE_SUB_KEYS = &H8&
  48. Const KEY_NOTIFY = &H10&
  49. Const KEY_CREATE_LINK = &H20&
  50. Const READ_CONTROL = &H20000
  51. Const WRITE_DAC = &H40000
  52. Const WRITE_OWNER = &H80000
  53. Const SYNCHRONIZE = &H100000
  54. Const STANDARD_RIGHTS_REQUIRED = &HF0000
  55. Const STANDARD_RIGHTS_READ = READ_CONTROL
  56. Const STANDARD_RIGHTS_WRITE = READ_CONTROL
  57. Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
  58. Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
  59. Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
  60. Const KEY_EXECUTE = KEY_READ
  61.  
  62. Dim hKey As Long, MainKeyHandle As Long
  63. Dim rtn As Long, lBuffer As Long, sBuffer As String
  64. Dim lBufferSize As Long
  65. Dim lDataSize As Long
  66. Dim ByteArray() As Byte
  67.  
  68. 'This constant determins wether or not to display error messages to the
  69. 'user. I have set the default value to False as an error message can and
  70. 'does become irritating after a while. Turn this value to true if you want
  71. 'to debug your programming code when reading and writing to your system
  72. 'registry, as any errors will be displayed in a message box.
  73.  
  74. Const DisplayErrorMsg = False
  75.  
  76.  
  77. Function SetDWORDValue(Subkey As String, Entry As String, Value As Long)
  78.  
  79. Call ParseKey(Subkey, MainKeyHandle)
  80.  
  81. If MainKeyHandle Then
  82.    rtn = RegOpenKeyEx(MainKeyHandle, Subkey, 0, KEY_WRITE, hKey) 'open the key
  83.    If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
  84.       rtn = RegSetValueExA(hKey, Entry, 0, REG_DWORD, Value, 4) 'write the value
  85.       If Not rtn = ERROR_SUCCESS Then   'if there was an error writting the value
  86.          If DisplayErrorMsg = True Then 'if the user want errors displayed
  87.             MsgBox ErrorMsg(rtn)        'display the error
  88.          End If
  89.       End If
  90.       rtn = RegCloseKey(hKey) 'close the key
  91.    Else 'if there was an error opening the key
  92.       If DisplayErrorMsg = True Then 'if the user want errors displayed
  93.          MsgBox ErrorMsg(rtn) 'display the error
  94.       End If
  95.    End If
  96. End If
  97.  
  98. End Function
  99. Function GetDWORDValue(Subkey As String, Entry As String)
  100.  
  101. Call ParseKey(Subkey, MainKeyHandle)
  102.  
  103. If MainKeyHandle Then
  104.    rtn = RegOpenKeyEx(MainKeyHandle, Subkey, 0, KEY_READ, hKey) 'open the key
  105.    If rtn = ERROR_SUCCESS Then 'if the key could be opened then
  106.       rtn = RegQueryValueExA(hKey, Entry, 0, REG_DWORD, lBuffer, 4) 'get the value from the registry
  107.       If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
  108.          rtn = RegCloseKey(hKey)  'close the key
  109.          GetDWORDValue = lBuffer  'return the value
  110.       Else                        'otherwise, if the value couldnt be retreived
  111.          GetDWORDValue = "Error"  'return Error to the user
  112.          If DisplayErrorMsg = True Then 'if the user wants errors displayed
  113.             MsgBox ErrorMsg(rtn)        'tell the user what was wrong
  114.          End If
  115.       End If
  116.    Else 'otherwise, if the key couldnt be opened
  117.       GetDWORDValue = "Error"        'return Error to the user
  118.       If DisplayErrorMsg = True Then 'if the user wants errors displayed
  119.          MsgBox ErrorMsg(rtn)        'tell the user what was wrong
  120.       End If
  121.    End If
  122. End If
  123.  
  124. End Function
  125. Function SetBinaryValue(Subkey As String, Entry As String, Value As String)
  126. Dim i As Integer
  127. Call ParseKey(Subkey, MainKeyHandle)
  128.  
  129. If MainKeyHandle Then
  130.    rtn = RegOpenKeyEx(MainKeyHandle, Subkey, 0, KEY_WRITE, hKey) 'open the key
  131.    If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
  132.       lDataSize = Len(Value)
  133.       ReDim ByteArray(lDataSize)
  134.       For i = 1 To lDataSize
  135.       ByteArray(i) = Asc(Mid$(Value, i, 1))
  136.       Next
  137.       rtn = RegSetValueExB(hKey, Entry, 0, REG_BINARY, ByteArray(1), lDataSize) 'write the value
  138.       If Not rtn = ERROR_SUCCESS Then   'if the was an error writting the value
  139.          If DisplayErrorMsg = True Then 'if the user want errors displayed
  140.             MsgBox ErrorMsg(rtn)        'display the error
  141.          End If
  142.       End If
  143.       rtn = RegCloseKey(hKey) 'close the key
  144.    Else 'if there was an error opening the key
  145.       If DisplayErrorMsg = True Then 'if the user wants errors displayed
  146.          MsgBox ErrorMsg(rtn) 'display the error
  147.       End If
  148.    End If
  149. End If
  150.  
  151. End Function
  152.  
  153.  
  154. Function GetBinaryValue(Subkey As String, Entry As String)
  155.  
  156. Call ParseKey(Subkey, MainKeyHandle)
  157.  
  158. If MainKeyHandle Then
  159.    rtn = RegOpenKeyEx(MainKeyHandle, Subkey, 0, KEY_READ, hKey) 'open the key
  160.    If rtn = ERROR_SUCCESS Then 'if the key could be opened
  161.       lBufferSize = 1
  162.       rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, 0, lBufferSize) 'get the value from the registry
  163.       sBuffer = Space(lBufferSize)
  164.       rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, sBuffer, lBufferSize) 'get the value from the registry
  165.       If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
  166.          rtn = RegCloseKey(hKey)  'close the key
  167.          GetBinaryValue = sBuffer 'return the value to the user
  168.       Else                        'otherwise, if the value couldnt be retreived
  169.          GetBinaryValue = "Error" 'return Error to the user
  170.          If DisplayErrorMsg = True Then 'if the user wants to errors displayed
  171.             MsgBox ErrorMsg(rtn)  'display the error to the user
  172.          End If
  173.       End If
  174.    Else 'otherwise, if the key couldnt be opened
  175.       GetBinaryValue = "Error" 'return Error to the user
  176.       If DisplayErrorMsg = True Then 'if the user wants to errors displayed
  177.          MsgBox ErrorMsg(rtn)  'display the error to the user
  178.       End If
  179.    End If
  180. End If
  181.  
  182. End Function
  183. Function DeleteKey(Keyname As String)
  184.  
  185. Call ParseKey(Keyname, MainKeyHandle)
  186.  
  187. If MainKeyHandle Then
  188.    rtn = RegOpenKeyEx(MainKeyHandle, Keyname, 0, KEY_WRITE, hKey) 'open the key
  189.    If rtn = ERROR_SUCCESS Then 'if the key could be opened then
  190.       rtn = RegDeleteKey(hKey, Keyname) 'delete the key
  191.       rtn = RegCloseKey(hKey)  'close the key
  192.    End If
  193. End If
  194.  
  195. End Function
  196.  
  197. Function GetMainKeyHandle(MainKeyName As String) As Long
  198.  
  199. Const HKEY_CLASSES_ROOT = &H80000000
  200. Const HKEY_CURRENT_USER = &H80000001
  201. Const HKEY_LOCAL_MACHINE = &H80000002
  202. Const HKEY_USERS = &H80000003
  203. Const HKEY_PERFORMANCE_DATA = &H80000004
  204. Const HKEY_CURRENT_CONFIG = &H80000005
  205. Const HKEY_DYN_DATA = &H80000006
  206.    
  207. Select Case MainKeyName
  208.        Case "HKEY_CLASSES_ROOT"
  209.             GetMainKeyHandle = HKEY_CLASSES_ROOT
  210.        Case "HKEY_CURRENT_USER"
  211.             GetMainKeyHandle = HKEY_CURRENT_USER
  212.        Case "HKEY_LOCAL_MACHINE"
  213.             GetMainKeyHandle = HKEY_LOCAL_MACHINE
  214.        Case "HKEY_USERS"
  215.             GetMainKeyHandle = HKEY_USERS
  216.        Case "HKEY_PERFORMANCE_DATA"
  217.             GetMainKeyHandle = HKEY_PERFORMANCE_DATA
  218.        Case "HKEY_CURRENT_CONFIG"
  219.             GetMainKeyHandle = HKEY_CURRENT_CONFIG
  220.        Case "HKEY_DYN_DATA"
  221.             GetMainKeyHandle = HKEY_DYN_DATA
  222. End Select
  223.  
  224. End Function
  225.  
  226. Function ErrorMsg(lErrorCode As Long) As String
  227.     
  228. 'If an error does accurr, and the user wants error messages displayed, then
  229. 'display one of the following error messages
  230. Dim GetErrorMsg As String
  231. Select Case lErrorCode
  232.        Case 1009, 1015
  233.             GetErrorMsg = "The Registry Database is corrupt!"
  234.        Case 2, 1010
  235.             GetErrorMsg = "Bad Key Name"
  236.        Case 1011
  237.             GetErrorMsg = "Can't Open Key"
  238.        Case 4, 1012
  239.             GetErrorMsg = "Can't Read Key"
  240.        Case 5
  241.             GetErrorMsg = "Access to this key is denied"
  242.        Case 1013
  243.             GetErrorMsg = "Can't Write Key"
  244.        Case 8, 14
  245.             GetErrorMsg = "Out of memory"
  246.        Case 87
  247.             GetErrorMsg = "Invalid Parameter"
  248.        Case 234
  249.             GetErrorMsg = "There is more data than the buffer has been allocated to hold."
  250.        Case Else
  251.             GetErrorMsg = "Undefined Error Code:  " & Str$(lErrorCode)
  252. End Select
  253.  
  254. End Function
  255.  
  256.  
  257.  
  258. Public Function AGetStringValue(Subkey As String, Entry As String)
  259.  
  260. Call ParseKey(Subkey, MainKeyHandle)
  261.  
  262. If MainKeyHandle Then
  263.    rtn = RegOpenKeyEx(MainKeyHandle, Subkey, 0, KEY_READ, hKey) 'open the key
  264.    If rtn = ERROR_SUCCESS Then 'if the key could be opened then
  265.       sBuffer = Space(255)     'make a buffer
  266.       lBufferSize = Len(sBuffer)
  267.       rtn = RegQueryValueEx(hKey, Entry, 0, REG_SZ, sBuffer, lBufferSize) 'get the value from the registry
  268.       If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
  269.          rtn = RegCloseKey(hKey)  'close the key
  270.          sBuffer = Trim(sBuffer)
  271.          AGetStringValue = Left(sBuffer, Len(sBuffer) - 1) 'return the value to the user
  272.       Else                        'otherwise, if the value couldnt be retreived
  273.          AGetStringValue = "" 'return Error to the user
  274.          If DisplayErrorMsg = True Then 'if the user wants errors displayed then
  275.             MsgBox ErrorMsg(rtn)  'tell the user what was wrong
  276.          End If
  277.       End If
  278.    Else 'otherwise, if the key couldnt be opened
  279.       AGetStringValue = ""       'return Error to the user
  280.       If DisplayErrorMsg = True Then 'if the user wants errors displayed then
  281.          MsgBox ErrorMsg(rtn)        'tell the user what was wrong
  282.       End If
  283.    End If
  284. End If
  285.  
  286. End Function
  287.  
  288. Private Sub ParseKey(Keyname As String, Keyhandle As Long)
  289.     
  290. rtn = InStr(Keyname, "\") 'return if "\" is contained in the Keyname
  291.  
  292. If Left(Keyname, 5) <> "HKEY_" Or Right(Keyname, 1) = "\" Then 'if the is a "\" at the end of the Keyname then
  293.    MsgBox "Incorrect Format:" + Chr(10) + Chr(10) + Keyname 'display error to the user
  294.    Exit Sub 'exit the procedure
  295. ElseIf rtn = 0 Then 'if the Keyname contains no "\"
  296.    Keyhandle = GetMainKeyHandle(Keyname)
  297.    Keyname = "" 'leave Keyname blank
  298. Else 'otherwise, Keyname contains "\"
  299.    Keyhandle = GetMainKeyHandle(Left(Keyname, rtn - 1)) 'seperate the Keyname
  300.    Keyname = Right(Keyname, Len(Keyname) - rtn)
  301. End If
  302.  
  303. End Sub
  304. Function CreateKey(Subkey As String)
  305.  
  306. Call ParseKey(Subkey, MainKeyHandle)
  307.  
  308. If MainKeyHandle Then
  309.    rtn = RegCreateKey(MainKeyHandle, Subkey, hKey) 'create the key
  310.    If rtn = ERROR_SUCCESS Then 'if the key was created then
  311.       rtn = RegCloseKey(hKey)  'close the key
  312.    End If
  313. End If
  314.  
  315. End Function
  316. Function SetStringValue(Subkey As String, Entry As String, Value As String)
  317.  
  318. Call ParseKey(Subkey, MainKeyHandle)
  319.  
  320. If MainKeyHandle Then
  321.    rtn = RegOpenKeyEx(MainKeyHandle, Subkey, 0, KEY_WRITE, hKey) 'open the key
  322.    If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
  323.       rtn = RegSetValueEx(hKey, Entry, 0, REG_SZ, ByVal Value, Len(Value)) 'write the value
  324.       If Not rtn = ERROR_SUCCESS Then   'if there was an error writting the value
  325.          If DisplayErrorMsg = True Then 'if the user wants errors displayed
  326.             MsgBox ErrorMsg(rtn)        'display the error
  327.          End If
  328.       End If
  329.       rtn = RegCloseKey(hKey) 'close the key
  330.    Else 'if there was an error opening the key
  331.       If DisplayErrorMsg = True Then 'if the user wants errors displayed
  332.          MsgBox ErrorMsg(rtn)        'display the error
  333.       End If
  334.    End If
  335. End If
  336.  
  337. End Function
  338. Public Function GetStringValue(Subkey As String, Entry As String) As String
  339. Dim MemString As String
  340. MemString = AGetStringValue(Subkey, Entry)
  341. If InStr(MemString, Chr(0)) Then
  342.     GetStringValue = Left(MemString, InStr(MemString, Chr(0)) - 1)
  343. Else
  344.     GetStringValue = MemString
  345. End If
  346. End Function
  347.